package cc.rengu.redp.common.utils;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import lombok.extern.slf4j.Slf4j;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;

/**
 * 时间工具类
 */
@Slf4j
public class DateUtil {

    public static final String FULL_TIME_PATTERN = "yyyyMMddHHmmss";

    public static final String DATE_PATTERN_EIGHT = "yyyyMMdd";

    /**
     * 24小时制
     */
    public static final String FULL_TIME_SPLIT_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static final String FULL_TIME_WITH_MILL_SPLIT_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS";

    public static final String DATE_PATTERN = "yyyy-MM-dd";

    public static final String DATE_PATTERN_8 = "yyyyMMdd";

    public static final String TIME_PATTERN = "HH:mm:ss";

    public static final String TIME_PATTERN_8 = "hhmmss";

    private static final SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN_8);
    private static final DateTimeFormatter dtfFullTime = DateTimeFormatter.ofPattern(FULL_TIME_SPLIT_PATTERN);

    public static String formatFullTime(LocalDateTime localDateTime) {
        return formatFullTime(localDateTime, FULL_TIME_PATTERN);
    }

    public static String formatFullTime(LocalDateTime localDateTime, String pattern) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        return localDateTime.format(dateTimeFormatter);
    }

    public static String getDateFormat(Date date, String dateFormatType) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormatType);
        return sdf.format(date);
    }

    public static String getLocalDateFormat(LocalDate localDate, String pattern) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        return localDate.format(dateTimeFormatter);
    }

    public static String getHumanDisplayForTimediff(Long diffMillis) {
        if (diffMillis == null) {
            return "";
        } else {
            long day = diffMillis / 86400000L;
            long hour = diffMillis / 3600000L - day * 24L;
            long min = diffMillis / 60000L - day * 24L * 60L - hour * 60L;
            long se = diffMillis / 1000L - day * 24L * 60L * 60L - hour * 60L * 60L - min * 60L;
            StringBuilder sb = new StringBuilder();
            if (day > 0L) {
                sb.append(day + "D:");
            }

            DecimalFormat df = new DecimalFormat("00");
            sb.append(df.format(hour) + ":");
            sb.append(df.format(min) + ":");
            sb.append(df.format(se));
            return sb.toString();
        }
    }

    public static LocalDateTime dateToLocalDateTime(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static LocalDateTime longToLocalDateTime(long time) {
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault());
    }

    public static long LocalDateTimeToLong(LocalDateTime localDateTime) {
        ZoneId zone = ZoneId.systemDefault();
        return localDateTime.atZone(zone).toInstant().toEpochMilli();
    }

    public static Date localDateToDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 把日期字串转换为LocalDateTime
     * @param dateTime  支持yyyy-MM-dd (自动补时间23:59:59), yyyymmdd (自动补时间23:59:59), yyyy-MM-dd HH:mm:ss 格式
     * @return
     */
    public static LocalDateTime stringToLocalDateTime(String dateTime) {
        String dtTrim = dateTime.trim();
        String dtString;
        if (dtTrim.length() == 19) {
            dtString = dateTime;
        } else if (dtTrim.length() == 10) {
            dtString = dtTrim.concat(" 23:59:59");
        } else if (dtTrim.length() == 8) {
            StringBuilder sb = new StringBuilder(dtTrim);
            sb.insert(4, StringPool.DASH);
            sb.insert(6, StringPool.DASH);
            sb.append(" 23:59:59");
            dtString = sb.toString();
        } else {
            return null;
        }

        LocalDateTime ldt;
        try {
            ldt = LocalDateTime.parse(dtString, dtfFullTime);
        } catch (Exception e) {
            return null;
        }
        return ldt;
    }

    /**
     * 格式化Date为yyyyMMdd
     *
     * @param date
     * @return
     */
    public static String formatDate(Date date) {
        return sdf.format(date);
    }

    /**
     * 格式化Date为yyyyMMdd
     *
     * @param strDate
     * @return
     */
    public static Date formatStrDate(String strDate) {
        Date date = null;
        try {
            date = sdf.parse(strDate);
        } catch (ParseException e) {
            log.error("formatStrDate error [{}]", e);
        }

        return date;
    }
}
